<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\index\model\Good;
class Goods extends Controller
{
   public function index()
  {
    return view("index");
  }
    public function insert()
  {
    $request = Request::instance();
    $data=$request->post();
    //返回值 
    $photo=$this->upload('photo');
    //入库
    $data['photo'] = 'uploads/'.$photo;
    $recetion = new Good;
    $res=$recetion->insertData($data);
    if($res){
    	 $this->success("添加成功",'goods/show');
      //  $this->success("添加成功");
    }else{
        $this->error("添加失败");
    }
  }
public function upload($photo){
   // 获取表单上传文件 例如上传了001.jpg
   $file = request()->file($photo);
    // 移动到框架应用根目录/public/uploads/ 目录下
   $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
    if($info){
        // 成功上传后 获取上传信息
        // 输出 jpg
        // echo $info->getExtension();
        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
        return $info->getSaveName();
        // 输出 42a79759f284b767dfcb2a0197904287.jpg
        // return $info->getFilename(); 
    }else{
        // 上传失败获取错误信息
       return $file->getError();
    }
}


  public function show(){
     $recetion =new Good;
     $data=$recetion->show();
     return $this->fetch('show',['data'=>$data]);
  }
  public function delete(){
    $request=Request::instance();
    $id=$request->get('id');
    $recetion=new Good;
    $res=$recetion->deleteData($id);
    if($res){
        $this->success("删除成功",'goods/show');
    }else{
        $this->error("删除失败");
    }
  }
  public function xui(){
    $request=Request::instance();
    $id=$request->get('id');
    $recetion=new Good;
    $res=$recetion->findData($id);
    // return view("updata",);
  }

}


?>